<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拦截器</title>
    <script src="../node_modules/axios/dist/mine-axios.js"></script>
</head>
<body>
    <script type="text/javascript">
        console.dir(axios);
        // 设置请求拦截器
        axios.interceptors.request.use(function one(config) {
            console.log("request interceptor success-1");
            // 修改config中的参数
            config.params = {a: 100};
            return config;
        }, function one(error) {
            console.log("request interceptor failure-1");
            return Promise.reject(error);
        });
        axios.interceptors.request.use(function two(config) {
            console.log("request interceptor success-2");
            // 修改config中的参数
            config.timeout = 2000;
            return config;
        }, function two(error) {
            console.log("request interceptor failure-2");
            return Promise.reject(error);
        });

        // 设置响应拦截器
        axios.interceptors.response.use(function (response) {
            console.log("response interceptor success-1");
            // console.log(response);
            // console.log(response.data);
            // return response;
            return response.data;
        }, function (error) {
            console.log("response interceptor failure-1");
            return Promise.reject(error);
        });

        axios.interceptors.response.use(function (response) {
            console.log("response interceptor success-2");
            return response;
        }, function (error) {
            console.log("response interceptor failure-2");
            return Promise.reject(error);
        });

        // 发送请求
        axios({
            method: 'GET',
            url: 'http://localhost:3000/posts'
        }).then(resp => {
            console.log('自定义回调，处理成功的结果');
            console.log(resp);
        }).catch((reason)=>{
            console.log('自定义失败回调');
        });
    </script>
</body>
</html>